/*
  Copyright (c) 2010 Xynilex Project

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
 */

// OGRE Headers
#include "Ogre.h"

// MyGUI Headers
#include "MyGUI.h"
#include "MyGUI_OgrePlatform.h"

// Xynilex Headers
#include "Common/Asserts.h"
#include "GUI/GUIManager.h"

using Xynilex::GUI::GUIManager;

GUIManager::GUIManager()
{
  this->mGUI = new MyGUI::Gui();
  this->mPlatform = new MyGUI::OgrePlatform();
  this->mInputManager = MyGUI::InputManager::getInstancePtr();
}

GUIManager::~GUIManager()
{
  this->shutdown();
}

void GUIManager::initialize(const Ogre::RenderWindow* window, const Ogre::SceneManager* sceneManager)
{
  this->mPlatform->initialise(const_cast<Ogre::RenderWindow*>(window), const_cast<Ogre::SceneManager*>(sceneManager));
  this->mGUI->initialise("../Media/Skins/Default/core.xml");
}

void GUIManager::shutdown()
{
  if(this->mGUI != NULL)
  {
    this->mGUI->shutdown();
    delete this->mGUI;
    this->mGUI = NULL;
  }

  if(this->mPlatform != NULL)
  {
    this->mPlatform->shutdown();
    delete this->mPlatform;
    this->mPlatform = NULL;
  }
}

void GUIManager::loadLayout(const std::string& name)
{
  XyAssert(this->mGUI != NULL);

  MyGUI::ResourceManager::getInstance().load(name);
  MyGUI::LayoutManager::getInstance().loadLayout(name);
}

bool GUIManager::injectMouseMove(int absX, int absY, int absZ)
{
  XyAssert(this->mInputManager != NULL);
  return this->mInputManager->injectMouseMove(absX, absY, absZ);
}

bool GUIManager::injectMousePressed(int absX, int absY, int button)
{
  XyAssert(this->mInputManager != NULL);
  return this->mInputManager->injectMousePress(absX, absY, MyGUI::MouseButton::Enum(button));
}

bool GUIManager::injectMouseReleased(int absX, int absY, int button)
{
  XyAssert(this->mInputManager != NULL);
  return this->mInputManager->injectMouseRelease(absX, absY, MyGUI::MouseButton::Enum(button));
}

bool GUIManager::injectKeyPressed(int key, char text)
{
  XyAssert(this->mInputManager != NULL);
  return this->mInputManager->injectKeyPress(MyGUI::KeyCode::Enum(key), text);
}

bool GUIManager::injectKeyReleased(int key)
{
  XyAssert(this->mInputManager != NULL);
  return this->mInputManager->injectKeyRelease(MyGUI::KeyCode::Enum(key));
}
